package com.gitee.fsmxm.thinking.in.spring.ioc.overview.dependency.injection;

import com.gitee.fsmxm.thinking.in.spring.ioc.overview.annotation.Super;
import com.gitee.fsmxm.thinking.in.spring.ioc.overview.domain.User;
import com.gitee.fsmxm.thinking.in.spring.ioc.overview.repository.UserRepository;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.env.Environment;

import java.util.Map;

/**
 * 依赖注入demo
 * 手动注入
 * 自动注入
 */
public class DependencyInjectionDemo {

    public static void main(String[] args) {

        // 启动spring 上下文
        // 或者  classpath:/META-INF/...
        BeanFactory beanFactory = new ClassPathXmlApplicationContext("META-INF/dependency-injection-context.xml");
        UserRepository userRepository = (UserRepository) beanFactory.getBean("userRepository");
        System.out.println(userRepository);
        System.out.println(userRepository.getBeanFactory());
        System.out.println(userRepository.getObjectFactory());

        // （自定义bean）
        System.out.println("----------");
        UserRepository userRepositoryAutoWiring = (UserRepository) beanFactory.getBean("userRepositoryAutoWiring");
        System.out.println(userRepositoryAutoWiring);
        // 依赖注入 （内建依赖）
        System.out.println(userRepositoryAutoWiring.getBeanFactory());
        // 自动注入的BeanFactory并不是当前的beanFactory
        System.out.println(userRepositoryAutoWiring.getBeanFactory() == beanFactory);
        System.out.println(beanFactory);
        // 依赖查找不到 （错误）
        //System.out.println(beanFactory.getBean(BeanFactory.class));
        System.out.println("---------");
        // 泛型依赖注入
        System.out.println(userRepositoryAutoWiring.getObjectFactory());
        System.out.println(userRepositoryAutoWiring.getObjectFactory().getObject());
        System.out.println(userRepositoryAutoWiring.getApplicationContextObjectFactory());
        System.out.println(userRepositoryAutoWiring.getApplicationContextObjectFactory().getObject() == beanFactory);

        // （容器内建bean）
        System.out.println(beanFactory.getBean(Environment.class));
    }


}
